package com.ddwl.schema.bo.order;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author Fu Zhaohui
 * @date 2015-04-07
 */
@Setter
@Getter
@NoArgsConstructor
public class WxAuthBo {

    //以下字段在return_code为SUCCESS的时候有返回
    private String appid;//公众账号ID
    private String sub_appid; //子商户公众号ID
    private String mch_id;//商户号
    private String sub_mch_id;//子商户号
    private String mch_key;//商户密钥
    private String device_info;//设备号
    private String nonce_str;//随机字符串
    private String sign;//签名
    private String openid;//用户标识
    private String sub_openid;//子商户公众号openid
    private String is_subscribe;//是否关注公众账号;用户是否关注公众账号，Y-关注，N-未关注，仅在公众账号类型支付有效
    private String trade_type;//JSAPI、NATIVE、APP
    private String bank_type;//付款银行
    private Long total_fee;//订单总金额，单位为分
    private Long refund_fee;//退款总金额，单位为分
    private String fee_type;//货币种类;默认人民币：CNY
    private Long cash_fee;//现金支付金额
    private String cash_fee_type;//现金支付货币类型;默认人民币：CNY
    private String transaction_id;//微信支付订单号
    private String out_trade_no;//商户订单号
    private String out_refund_no;//商户退款订单号
    private String subject;
    private String attach;//商家数据包
    private String time_end;//支付完成时间
    private String body;//商品或支付单简要描述
    private String notify_url;//接收微信支付异步通知回调地址
    private String spbill_create_ip;//终端IP
    private String noncestr;
    private String time_start; // 交易起始时间
    private String time_expire; // 交易结束时间

    private String detail;
    private String goods_tag;
    private String auth_code;

    // 开放平台授权认证
    private String component_appid;
    private String component_appsecret;
    private String component_verify_ticket;
    private String authorization_code;
    private String authorizer_refresh_token;

    // 授权选项信息
    private String authorizer_appid;
    private String option_name;
    private String option_value;

    private String long_url; // C扫B二维码长链接 weixin://wxpay/bizpayurl?sign=XXXXX&appid=XXXXX&mch_id=XXXXX&product_id=XXXXXX&time_stamp=XXXXXX&nonce_str=XXXXX

    private Integer offset;
    private Integer count;

    // 单据ID或商品ID
    private String product_id;

    private String begin_time;
    private String end_time;


    public WxAuthBo(Map<String, String> params) {
        this.appid = params.get("appid");
        this.mch_id = params.get("mch_id");
        this.device_info = params.get("device_info");
        this.nonce_str = params.get("nonce_str");
        this.sign = params.get("sign");
        this.trade_type = params.get("trade_type");
        this.time_start = params.get("time_start");
        this.notify_url = params.get("notify_url");
        this.transaction_id = params.get("transaction_id");
        this.out_trade_no = params.get("out_trade_no");
        this.out_refund_no = params.get("out_refund_no");
        this.attach = params.get("attach");
        this.body = params.get("body");
        this.openid = params.get("openid");
        this.time_start = params.get("time_start");
        this.time_expire = params.get("time_expire");
        if (StringUtils.hasText(params.get("total_fee"))) {
            this.total_fee = Long.parseLong(params.get("total_fee"));
        }
        if (StringUtils.hasText(params.get("refund_fee"))) {
            this.refund_fee = Long.parseLong(params.get("refund_fee"));
        }
        this.detail = params.get("detail");
        this.goods_tag = params.get("goods_tag");
        this.auth_code = params.get("auth_code");
        this.sub_appid = params.get("sub_appid");
        this.sub_openid = params.get("sub_openid");
        this.sub_mch_id = params.get("sub_mch_id");
        this.begin_time = params.get("begin_time");
        this.end_time = params.get("end_time");
        if (StringUtils.hasText(params.get("offset"))) {
            this.offset = Integer.parseInt(params.get("offset"));
        }
    }

    public WxAuthBo(String appId, String appSecret, String verifyTicket) {
        this.component_appid = appId;
        this.component_appsecret = appSecret;
        this.component_verify_ticket = verifyTicket;
    }

    public WxAuthBo(String component_appid) {
        this.component_appid = component_appid;
    }

    public WxAuthBo(String component_appid, String authorization_code) {
        this.component_appid = component_appid;
        this.authorization_code = authorization_code;
    }
}